package com.giscloud.limp.service.impl;

import com.alibaba.fastjson.JSON;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.limp.dao.LegalPublicityDao;
import com.giscloud.limp.entity.LegalPublicity;
import com.giscloud.limp.entity.LegalPublicityExample;
import com.giscloud.limp.entity.dto.LegalPublicityDTO;
import com.giscloud.limp.service.LegalPublicityService;
import com.giscloud.token.utils.TokenUserUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service("legalPublicityService")
@Log4j2
public class LegalPublicityServiceImpl implements LegalPublicityService {
    private final  LegalPublicityDao legalPublicityDao;
    @Autowired
    private TokenUserUtil tokenUserUtil;
    @Autowired
    public LegalPublicityServiceImpl(LegalPublicityDao legalPublicityDao) {
        this.legalPublicityDao = legalPublicityDao;
    }
    /**
    *
    * 列表
    */
    @Override
    public PageInfo queryPage(LegalPublicityDTO legalPublicityDTO) {
        log.debug("#queryPage parameter ={}", JSON.toJSONString(legalPublicityDTO));
        PageHelper.startPage(legalPublicityDTO.getPage(), legalPublicityDTO.getRows());
        LegalPublicityExample legalPublicityExample = new LegalPublicityExample();
        if(StringUtils.isNotBlank(legalPublicityDTO.getTitle())){
            legalPublicityExample.or().andTitleLike("%"+legalPublicityDTO.getTitle()+"%")
                    .andLegalPublicityTypeEqualTo(legalPublicityDTO.getLegalPublicityType()).andStatusCdEqualTo(1);
            legalPublicityExample.or().andContentLike("%"+legalPublicityDTO.getContent()+"%")
                    .andLegalPublicityTypeEqualTo(legalPublicityDTO.getLegalPublicityType()).andStatusCdEqualTo(1);
        }else{
            legalPublicityExample.createCriteria().andLegalPublicityTypeEqualTo(legalPublicityDTO.getLegalPublicityType()).andStatusCdEqualTo(1);
        }
        legalPublicityExample.setOrderByClause("legal_publicity_id desc");
        List<LegalPublicity> legalPublicitys = legalPublicityDao.selectByExample(legalPublicityExample);
        return PageInfo.of(legalPublicitys);
    }


    /**
     * 信息
     */
    @Override
    public LegalPublicity getById(Integer legalPublicityId){
        log.debug("#getById parameter ={}", legalPublicityId);
        LegalPublicity legalPublicity = legalPublicityDao.selectByPrimaryKey(legalPublicityId);
        PageHelper.startPage(1, 1);
        LegalPublicityExample preLegalPublicityExample = new LegalPublicityExample();
        preLegalPublicityExample.createCriteria().andLegalPublicityTypeEqualTo(legalPublicity.getLegalPublicityType())
                .andStatusCdEqualTo(1).andLegalPublicityIdLessThan(legalPublicityId);
        preLegalPublicityExample.setOrderByClause("legal_publicity_id desc");
        List<LegalPublicity> preLegalPublicitys = legalPublicityDao.selectByExample(preLegalPublicityExample);
        if(CollectionUtils.isNotEmpty(preLegalPublicitys)){
            legalPublicity.setNextId(preLegalPublicitys.get(0).getLegalPublicityId());
            legalPublicity.setNextTitle(preLegalPublicitys.get(0).getTitle());

        }
        PageHelper.startPage(1, 1);
        LegalPublicityExample nextLegalPublicityExample = new LegalPublicityExample();
        nextLegalPublicityExample.createCriteria().andLegalPublicityTypeEqualTo(legalPublicity.getLegalPublicityType())
                .andStatusCdEqualTo(1).andLegalPublicityIdGreaterThan(legalPublicityId);
        nextLegalPublicityExample.setOrderByClause("legal_publicity_id asc");
        List<LegalPublicity> nextLegalPublicitys = legalPublicityDao.selectByExample(nextLegalPublicityExample);
        if(CollectionUtils.isNotEmpty(nextLegalPublicitys)){
            legalPublicity.setPrevId(nextLegalPublicitys.get(0).getLegalPublicityId());
            legalPublicity.setPrevTitle(nextLegalPublicitys.get(0).getTitle());
        }
        return legalPublicity;
    }

    /**
     * 保存
     */
    @Override
    public Result save(LegalPublicity legalPublicity){
        log.debug("#save parameter ={}", JSON.toJSONString(legalPublicity));
        legalPublicity.setStatusCd(1);
        legalPublicity.setCreatedDt(new Date());
        legalPublicity.setCreatedStaffId(tokenUserUtil.getUserId().intValue());
        legalPublicity.setPublisher(tokenUserUtil.getStaffName());
        if(legalPublicityDao.insertSelective(legalPublicity) > 0 ){
            return Result.success("保存成功");
        }else{
            return Result.error("保存失败");
        }
    }

    /**
     * 修改
     */
    @Override
    public Result updateById(LegalPublicity legalPublicity){
         log.debug("#updateById parameter ={}", JSON.toJSONString(legalPublicity));
         LegalPublicityExample legalPublicityExample = new LegalPublicityExample();
         LegalPublicityExample.Criteria criteria = legalPublicityExample.createCriteria();
         criteria.andLegalPublicityIdEqualTo(legalPublicity.getLegalPublicityId());
        legalPublicity.setOperateStaffId(tokenUserUtil.getUserId().intValue());
        legalPublicity.setPublisher(tokenUserUtil.getStaffName());
        legalPublicity.setVersion(new Date());
         if(legalPublicityDao.updateByExampleSelective(legalPublicity,legalPublicityExample) > 0){
             return Result.success("修改成功");
         }else{
             return Result.error("修改失败");
         }
    }
    /**
     * 删除
     */
    @Override
    public Result removeByIds(List<Integer> legalPublicityIds){
        log.debug("#removeByIds parameter ={}", JSON.toJSONString(legalPublicityIds));
        LegalPublicityExample legalPublicityExample = new LegalPublicityExample();
        LegalPublicityExample.Criteria criteria = legalPublicityExample.createCriteria();

            criteria.andLegalPublicityIdIn(legalPublicityIds);

        if(legalPublicityDao.deleteByExample(legalPublicityExample) > 0){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }

}